package com.zhangtai.modules.service.impl.security;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.controller.admin.JobController;
import com.zhangtai.modules.dao.FundsRecordDao;
import com.zhangtai.modules.dto.FundsRecordDto;
import com.zhangtai.modules.dto.FundsRecordReportDto;
import com.zhangtai.modules.dto.SecurityRecordReportDto;
import com.zhangtai.modules.entity.security.FundsRecordEntity;
import com.zhangtai.modules.entity.security.SecurityRecordEntity;
import com.zhangtai.modules.service.security.FundsRecordService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;


@RestController
public class FundsRecordServiceImpl extends ServiceImpl<FundsRecordDao, FundsRecordEntity> implements FundsRecordService {
    @Autowired
    private FundsRecordDao fundsRecordDao;
    @Autowired
    private JobController jobControl;
    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<FundsRecordEntity> page = this.page(
                new Page<FundsRecordEntity>(pageNum,pageSize),
                new QueryWrapper<FundsRecordEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

//    @Override
//    public  PageResult<FundsRecordDto> getRecordList(@RequestParam(value = "time", required = false) String time,
//                                                     @RequestParam(value = "endTime", required = false) String endTime,
//                                                   @RequestParam(value = "isCheck", required = false) Integer isCheck,
//                                                   @RequestParam(value = "isConfirm", required = false) Integer isConfirm,
//                                                   @RequestParam(value = "type", required = false) Integer type,
//                                                   @RequestParam(value = "unitName", required = false) String unitName,
//                                                   @RequestParam(value = "cityName", required = false) String cityName,
//                                                   @RequestParam(value = "oid", required = false) String oid,
//                                                   @RequestParam(value = "name", required = false) String name,
//                                                   @RequestParam(value = "orgIds",required = false) List<Long> orgIds,
//                                                   @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
//                                                   @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
//
//        Page<FundsRecordDto> page = new Page(pageNum,pageSize);
//        List<FundsRecordDto> entities = fundsRecordDao.getRecord(time,endTime,isCheck,isConfirm,type,unitName,cityName,oid,name,orgIds,page);
//        page.setRecords(entities);
//        return new PageResult<FundsRecordDto>(page) ;
//    }

    @Override
    public  PageResult<FundsRecordDto> getRecordList(@RequestParam(value = "time", required = false) String time,
                                                     @RequestParam(value = "endTime", required = false) String endTime,
                                                     @RequestParam(value = "isCheck", required = false) Integer isCheck,
                                                     @RequestParam(value = "isConfirm", required = false) Integer isConfirm,
                                                     @RequestParam(value = "type", required = false) Integer type,
                                                     @RequestParam(value = "unitName", required = false) String unitName,
                                                     @RequestParam(value = "cityName", required = false) String cityName,
                                                     @RequestParam(value = "oid", required = false) String oid,
                                                     @RequestParam(value = "name", required = false) String name,
                                                     @RequestParam(value = "orgIds",required = false) List<Long> orgIds,
                                                     @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {

        Page<FundsRecordDto> page = new Page(pageNum,pageSize);
        List<FundsRecordDto> entities = fundsRecordDao.getRecord(time,endTime,isCheck,isConfirm,type,unitName,cityName,oid,name,orgIds,page);
        page.setRecords(entities);
        return new PageResult<FundsRecordDto>(page) ;
    }

    @Override
    public  PageResult<FundsRecordDto> getRecordListAuth(@RequestParam(value = "time", required = false) String time,
                                                     @RequestParam(value = "endTime", required = false) String endTime,
                                                     @RequestParam(value = "isCheck", required = false) Integer isCheck,
                                                     @RequestParam(value = "isConfirm", required = false) Integer isConfirm,
                                                     @RequestParam(value = "type", required = false) Integer type,
                                                     @RequestParam(value = "unitName", required = false) String unitName,
                                                     @RequestParam(value = "cityName", required = false) String cityName,
                                                     @RequestParam(value = "oid", required = false) String oid,
                                                     @RequestParam(value = "name", required = false) String name,
                                                     @RequestParam(value = "orgIds",required = false) List<Long> orgIds,
                                                     @RequestParam(value = "contractIds",required = false) List<Long> contractIds,
                                                         @RequestParam(value = "unitId",required = false) Long unitId,
                                                     @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {

        Page<FundsRecordDto> page = new Page(pageNum,pageSize);
        List<FundsRecordDto> entities = fundsRecordDao.getReportRecordAuth(time,endTime,isCheck,isConfirm,type,unitName,cityName,oid,name,orgIds,contractIds,unitId,page);
        page.setRecords(entities);
        return new PageResult<FundsRecordDto>(page) ;
    }

    @Override
    public  PageResult<FundsRecordReportDto> getRecordReportList(@RequestParam(value = "contractIds",required = false) List<Long> contractIds,
            @RequestParam(value = "time", required = false) String time,
                                                     @RequestParam(value = "endTime", required = false) String endTime,
                                                     @RequestParam(value = "isConfirm", required = false) Integer isConfirm,
                                                     @RequestParam(value = "unitName", required = false) String unitName,
                                                     @RequestParam(value = "name", required = false) String name,
                                                     @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {

        Page<FundsRecordReportDto> page = new Page(pageNum,pageSize);
        List<FundsRecordReportDto> entities = fundsRecordDao.getReportRecord(contractIds,time,endTime,isConfirm,unitName,name,page);
        if(!CollectionUtils.isEmpty(entities)){
            for (FundsRecordReportDto item:entities
                    ) {
                if(item.getJobSystemId()!=null) {
                    item.setJobSystem(jobControl.systemMap.get(item.getJobSystemId()));
                }
                if(item.getOrgId()!=null){
                    item.setOrgName(jobControl.orgMap.get(item.getOrgId()));
                }
                if(item.getUnitId()!=null){
                    item.setDepartMentName(jobControl.orgMap.get(item.getDepartMentName()));
                }


            }

        }
        page.setRecords(entities);
        return new PageResult<FundsRecordReportDto>(page) ;
    }

    @Override
    public void removeUnCheckEntity(Long oid,String startDay,String endDay){
        List<FundsRecordEntity> list = this.list(new QueryWrapper<FundsRecordEntity>().
//                between("operate_time",startDay,endDay).
                eq("is_delete",0).eq("is_check",0).eq("oid",oid));
        for(FundsRecordEntity record:list){
            removeOneById(record.getRecordId());
        }
    }

    @Override
    public int getConfirmRecordCount(String securityId,String startTime,String endTime,Integer type){
        return fundsRecordDao.getConfirmRecordCount(startTime,endTime,securityId,type);
    }

    @Override
    public void deleteAllRecordBySecurityId(Long securityId){
       fundsRecordDao.deleteAllRecordBySecurityId(securityId);
    }

    @Override
    public List<String> getIdBySecurityId(Long securityId,Integer status){
        return fundsRecordDao.getAllSecurity(securityId,status);
    }
    @Override
    public FundsRecordEntity getByOidAndType(Long oid, Integer isCheck, String startDay, String endDay){
        FundsRecordEntity one = this.getOne(new QueryWrapper<FundsRecordEntity>().
                eq("oid", oid).eq("is_check", isCheck).between("operate_time",startDay,endDay).eq("is_delete",0));

        return one;
    }

    @Override
    public FundsRecordEntity getByIdE(Long id) {
            FundsRecordEntity one = this.getOne(new QueryWrapper<FundsRecordEntity>().eq("record_id", id).eq("is_delete", 0));

        return one;
    }

    @Override
    public R saveE(FundsRecordEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok();

    }

    @Override
    public R updateByIdE(FundsRecordEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            FundsRecordEntity entity = new FundsRecordEntity();
        entity.setRecordId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }

}